// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#ifndef OpenMeca_Gui_UserItemDialogLinkBaseClassic_hpp
#define OpenMeca_Gui_UserItemDialogLinkBaseClassic_hpp

#include "OpenMeca/Gui/UserItemDialogInterface.hpp"
#include "OpenMeca/Gui/WidgetSelectItem.hpp"
#include "OpenMeca/Gui/UserItemDialogLinkBase.hpp"
#include "OpenMeca/Core/Singleton.hpp"

namespace OpenMeca
{
  namespace Gui
  {

    template <class Link>
    class UserItemDialogLinkBaseClassic :  public UserItemDialogInterface<Link>, 
					public Core::Singleton<UserItemDialogLinkBaseClassic <Link> >,
					public UserItemDialogLinkBase
    {
      
    public:
      UserItemDialogLinkBaseClassic();
      ~UserItemDialogLinkBaseClassic();
      bool Check();

    private:
      void Init();
      Item::LinkBase& GetCurrentLink();

    private:
      WidgetSelectItem<Core::AutoRegisteredPtr<Item::Body, Item::LinkBase> >* body1_;
      WidgetSelectItem<Core::AutoRegisteredPtr<Item::Body, Item::LinkBase> >* body2_;
      
    };


    template <class Link>
    inline 
    UserItemDialogLinkBaseClassic <Link>::UserItemDialogLinkBaseClassic()
      : UserItemDialogInterface<Link>(),
	UserItemDialogLinkBase(&UserItemDialogInterface<Link>::GetContainer())

    {
    }
    
    template <class Link>
    inline
    UserItemDialogLinkBaseClassic <Link>::~UserItemDialogLinkBaseClassic()
    {
    }
    
    template <class Link>
    inline void
    UserItemDialogLinkBaseClassic <Link>::Init()
    {
      UserItemDialogLinkBase::Init();
    }

    template <class Link>
    inline Item::LinkBase&
    UserItemDialogLinkBaseClassic <Link>::GetCurrentLink()
    {
      return UserItemDialogInterface<Link>::GetCurrentItem(); 
    }



    template <class Link>
    inline bool
    UserItemDialogLinkBaseClassic<Link>::Check()
    {


      if (!UserItemDialogLinkBase::Check())
	return false;

      // Particular problem when body1 and body2 were swaped !
        //     if (&UserItemDialogLinkBase::GetWidgetSelectItemBody1().GetSelectedItem() == 
	//   &UserItemDialogInterface<Link>::GetCurrentItem().GetBody2() && 

	//   &UserItemDialogLinkBase::GetWidgetSelectItemBody2().GetSelectedItem() == 
	//   &UserItemDialogInterface<Link>::GetCurrentItem().GetBody1())
	// {
	//   std::cout << "WARNING body1 and body2 are swaped, need to manage this case" << std::endl;
	//   assert(0); //TODO !!!!
	//   UserItemDialogInterface<Link>::GetCurrentItem().GetBody1Ptr().Unregister();
	//   UserItemDialogInterface<Link>::GetCurrentItem().GetBody2Ptr().Unregister();
	  
	//   UserItemDialogLinkBase::GetWidgetSelectItemBody1().GetCopy().SetNullPtr();
	//   UserItemDialogLinkBase::GetWidgetSelectItemBody2().GetCopy().SetNullPtr();
	// }

      UserItemDialogInterface<Link>::GetCurrentItem().GetBody1().GetDependentItems().UnCheckForDoublet();
      UserItemDialogInterface<Link>::GetCurrentItem().GetBody2().GetDependentItems().UnCheckForDoublet();      
      bool ok = UserItemDialog::Check(); 

      UserItemDialogInterface<Link>::GetCurrentItem().GetBody1().GetDependentItems().CheckForDoublet();
      UserItemDialogInterface<Link>::GetCurrentItem().GetBody2().GetDependentItems().CheckForDoublet();   

      return ok;

      
      
    }
   


  }
}
#endif
